###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/InstallPackage.pm.pl -> InstallPackage module                  #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::InstallPackage;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "Logs.pm.pl";

use File::Path;
use File::Copy;
use Cwd;
use Archive::Tar;
use strict;

require "ControlPanel/Output/InstallPackage.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "INSTALLPACKAGE");

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::InstallPackage->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(error => $in{'ERROR'});
  
  return 1;
}

###############################################################################
# view subroutine
sub view {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "INSTALLPACKAGE");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  my $Package = $SL::CGI->param('FORM_PACKAGE');
  
  unless ($Package) {
    $self->show(DB => $in{'DB'}, ERROR => "MISSING-PACKAGE");
    return 1;
  }

  my $TempFile = $SL::CGI->tmpFileName($Package);
  unless (-e $TempFile && ($Package =~ /\.tar$/i || $Package =~ /\.pak$/i)) {
    $self->show(DB => $in{'DB'}, ERROR => "INVALID-PACKAGE");
    return 1;
  }

  #----------------------------------------------------------------------#
  # Preparing data...                                                    #

  rmtree("$SYSTEM->{'TEMP_PATH'}/Package");
  mkdir("$SYSTEM->{'TEMP_PATH'}/Package", 0777)
    || &Error::CGIError("Error creating temporary directory. $!", "$SYSTEM->{'TEMP_PATH'}/Package");
  chmod(0777, "$SYSTEM->{'TEMP_PATH'}/Package");
  
  if ($Package =~ /\.tar$/i) {
    my $tar = Archive::Tar->new();
       $tar->read($TempFile, 0);
    
    my $cwd = cwd();
    chdir("$SYSTEM->{'TEMP_PATH'}/Package");
    my @files = $tar->list_files();
    $tar->extract(@files);
    chdir($cwd);
  } else {
    copy($TempFile, "$SYSTEM->{'TEMP_PATH'}/Package/install.pak")
      || &Error::CGIError("Error copying temporary file. $!", "$SYSTEM->{'TEMP_PATH'}/Package/install.pak");
  }
  
  eval("require \"$SYSTEM->{'TEMP_PATH'}/Package/install.pak\";");
  &Error::Error("CP", MESSAGE => "Error requiring package. $@") if ($@);
  
  my $PACK = Package->new();
  
  unless ($PACK->{'FIELDS'} && scalar(@{ $PACK->{'FIELDS'} }) >= 1) {
    $self->do(DB => $in{'DB'});
    return 1;
  }
  
  my %INPUT;
  
  $INPUT{'FIELDS'} = $PACK->{'FIELDS'};

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::InstallPackage->new();

  &Standard::PrintHTMLHeader();
  print $Skin->view(input => \%INPUT);
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "INSTALLPACKAGE");

  #----------------------------------------------------------------------#
  # Preparing data...                                                    #

  my $Skin = Skin::CP::InstallPackage->new();

  &Standard::PrintHTMLHeader();  

  unless ($SL::QUERY{'do'}) {
    print $Skin->do();
    return 1;
  }

  my $Log = Logs->new(PRINT => 1);
  
  my $Exit = eval {
    sub {
      $Log->Footer();
      exit;
    }
  };
  
  $Log->Header();

  $Log->Line("", "uplevel");
  $Log->Line("Beginning installation of package.\n\n", "element");
  
  $Log->Line("Checking temporary file... ", "element");
  unless (-e "$SYSTEM->{'TEMP_PATH'}/Package/install.pak") {
    $Log->Line("Error: Doesn't exist", "error");
    &$Exit;
  }
  eval("require \"$SYSTEM->{'TEMP_PATH'}/Package/install.pak\";");
  if ($@) {
    $Log->Line("Error: $@", "error");
    &$Exit;
  }
  $Log->Line("Done.", "success");
  
  my $PACK = Package->new();
  
  $Log->Line("Getting package information...", "element");
  $Log->Line("", "uplevel");
  $Log->Line("Package Name: $PACK->{'INFO'}->{'NAME'}", "element");
  $Log->Line("Package Author: $PACK->{'INFO'}->{'AUTHOR'}", "element");
  $Log->Line("Package Details: $PACK->{'INFO'}->{'DETAILS'}", "element");
  $Log->Line("", "downlevel");
  
  $Log->Line("Executing package...", "element");
  $Log->Line("", "uplevel");
  
  my %Input;
  if ($PACK->{'FIELDS'} && scalar(@{ $PACK->{'FIELDS'} }) >= 1) {
    foreach my $field (@{ $PACK->{'FIELDS'} }) {
      next unless ($field->[1]->{'name'});
      if ($field->[0] eq "textbox" || $field->[0] eq "textarea" || $field->[0] eq "radio") {
        $Input{ $field->[1]->{'name'} } = $SL::QUERY{'FORM_'.$field->[1]->{'name'}};
      } elsif ($field->[0] eq "checkbox") {
        if (scalar(@{ $field->[1]->{'items'} }) > 1) {
          $Input{ $field->[1]->{'name'} } = [ $SL::CGI->param('FORM_'.$field->[1]->{'name'}) ];
        } else {
          $Input{ $field->[1]->{'name'} } = $SL::QUERY{'FORM_'.$field->[1]->{'name'}};
        }
      } elsif ($field->[0] eq "selectbox") {
        if ($field->[1]->{'multiple'} && scalar(@{ $field->[1]->{'items'} }) > 1) {
          $Input{ $field->[1]->{'name'} } = [ $SL::CGI->param('FORM_'.$field->[1]->{'name'}) ];
        } else {
          $Input{ $field->[1]->{'name'} } = $SL::QUERY{'FORM_'.$field->[1]->{'name'}};
        }
      }        
    }
  }
    
  $PACK->Main($Log, $Exit, "$SYSTEM->{'TEMP_PATH'}/Package", \%Input);
  $Log->Line("", "downlevel");
  
  $Log->Line("Removing temporary files... ", "element");
  if (rmtree("$SYSTEM->{'TEMP_PATH'}/Package")) {
    $Log->Line("Done.", "success");
  } else {
    $Log->Line("Error: $!", "error");
  }

  $Log->Line("\n\n", "");
  $Log->Line("All done!", "element");
  $Log->Line("", "downlevel");

  &$Exit;
}

1;